
package com.joyo8.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

public class Util {

    public void logd(String msg) {
        Log.d(Constants.TAG, msg);
    }

    /**
     * get this cell phone location
     */
    public Location getLoction(Context context) {
        LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc != null) {
            double lat = loc.getLatitude();
            logd( "latitude: " + lat);
            double lng = loc.getLongitude();
            logd("longitude: " + lng);
        }else{
            logd("loc is null");
        }
        return loc;
    }

    /**
     * get this cell phone GPS on / off status
     * @param context
     */
    public boolean getGPSState(Context context) {
        boolean _ret = false;
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            logd("The device GPS is on");
            _ret = true;
        }else{
            logd("The device GPS is off");
            _ret = false;
        }
        return _ret;
    }
    
    /**
     * set sms speech
     * @param context
     * @param enable
     */
    public void setSpeechSMS(Context context, boolean enable) {
        SharedPreferences pref = context.getSharedPreferences(Constants.PRE_NAME,
                Context.MODE_WORLD_WRITEABLE);
        Editor editor = pref.edit();
        editor.putInt(Constants.PRE_T2S_STATE, (enable) ? context.getResources().getInteger(
                R.integer.t2s_enable) : context.getResources().getInteger(R.integer.t2s_disable));
        editor.commit();
    }
    
    /**
     * get sms speech state
     * @param context
     * @return 1 speech enable, otherwise disable
     */
    public boolean getSpeechSMSState(Context context){
        SharedPreferences pref = context.getSharedPreferences(Constants.PRE_NAME,
                Context.MODE_WORLD_READABLE);
        return (pref.getInt(Constants.PRE_T2S_STATE, 0)==1)?true:false;
    };

}
